package com.qigao.mall.web.controller.app.user;

import com.github.pagehelper.PageInfo;
import com.qigao.mall.api.WithDrawService;
import com.qigao.mall.commons.dto.WithDrawApplyDto;
import com.qigao.mall.commons.dto.WithDrawOrderResultDto;
import com.qigao.mall.commons.dto.WithDrawQueryDto;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.enums.ResultCode;
import com.qigao.mall.commons.exceptions.BusinessException;
import com.qigao.mall.web.utils.RequestUtils;
import com.qigao.mall.web.utils.ValidatorUtil;
import com.qigao.mall.web.vo.PageVo;
import com.qigao.mall.web.vo.app.WithDrawApplyVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author luiz
 * @Title: com.qigao.mall.web.controller.app
 * @ProjectName three-mall
 * @date 2020-06-2020-06-12 10:39
 * @Description:
 */
@RestController
@RequestMapping("/app/user/withDraw/")
@Api(tags = "App提现相关")
public class AppWithDrawController {
    private static Logger logger= LoggerFactory.getLogger(AppWithDrawController.class);
    @Resource
    WithDrawService withDrawService;

    //1.退款申请
    @ApiOperation("提现申请申请")
    @PostMapping("/apply")
    public Result applyWithDraw(@RequestBody WithDrawApplyVo withDrawApplyVo, HttpServletRequest request){
        try{
            ValidatorUtil.validateEntity(withDrawApplyVo);
            WithDrawApplyDto withDrawApplyDto=new WithDrawApplyDto();
            BeanUtils.copyProperties(withDrawApplyVo,withDrawApplyDto);
            withDrawApplyDto.setUserNo(RequestUtils.getCurrentUserNo(request));
            return withDrawService.applyWithDraw(withDrawApplyDto);
        }catch (BusinessException be){
            logger.error("提现申请异常:{}",be);
            return Result.newError(be.getCode(),be.getMsg());
        }catch (Exception e){
            logger.error("提现申请发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }
    //2.查询提现单列表
    @GetMapping("/findWithDrawPage")
    public Result<PageInfo<WithDrawOrderResultDto>> findWithDrawPage(@RequestBody PageVo pageVo, HttpServletRequest request){
        try{
            WithDrawQueryDto withDrawQueryDto=new WithDrawQueryDto();
            BeanUtils.copyProperties(pageVo,withDrawQueryDto);
            withDrawQueryDto.setOwnerNo(RequestUtils.getCurrentUserNo(request));
            return withDrawService.findWithDrawPage(withDrawQueryDto);
        }catch (Exception e){
            logger.error("提现单列表查询发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }

}
